package com.jiusem.cordova.wechat.wxapi;

import org.apache.cordova.*;

import com.tencent.mm.sdk.openapi.BaseReq;
import com.tencent.mm.sdk.openapi.BaseResp;
import com.tencent.mm.sdk.openapi.ConstantsAPI;
import com.tencent.mm.sdk.openapi.ShowMessageFromWX;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.mm.sdk.openapi.WXAppExtendObject;
import com.tencent.mm.sdk.openapi.WXMediaMessage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.jiusem.cordova.wechat.WechatShare;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler{
	private static final int TIMELINE_SUPPORTED_VERSION = 0x21020001;
	private Button gotoBtn, regBtn, launchBtn, checkBtn;
    private IWXAPI api;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String APP_ID = WechatShare.appID;
        api = WXAPIFactory.createWXAPI(this,APP_ID, false);  
        api.handleIntent(getIntent(), this);  
    }

	@Override
	public void onReq(BaseReq req) {
		switch (req.getType()) {
		case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
			goToGetMsg();		
			break;
		case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
			goToShowMsg((ShowMessageFromWX.Req) req);
			break;
		default:
			break;
		}
	}

	@Override
	public void onResp(BaseResp resp) {
		int result;
		
		switch (resp.errCode) {
		case BaseResp.ErrCode.ERR_OK:
			result = 1;
			break;
		case BaseResp.ErrCode.ERR_USER_CANCEL:
			result = 2;
			break;
		case BaseResp.ErrCode.ERR_AUTH_DENIED:
			result = 3;
			break;
		default:
			result = 4;
			break;
		}
		
		WechatShare.sendCallBack(result);
		finish();
	}
	
	private void goToGetMsg() {
		finish();
	}
	
	private void goToShowMsg(ShowMessageFromWX.Req showReq) {

		finish();
	}
}